package com.example.common.config.redisson;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 *
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class RedissonConfig {

    @Autowired RedisProperties redisProperties;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient () {
        // 创建配置
        Config config = new Config();
        // 必须添加连接协议
        SingleServerConfig serverConfig = config.useSingleServer();
        serverConfig.setAddress("redis://"+redisProperties.getHost()+":"+redisProperties.getPort());
        serverConfig.setDatabase(redisProperties.getDatabase());
        // 创建实例
        return Redisson.create(config);
    }

}
